package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.goods.GoodsInfo;
import com.example.demo.service.goods.GoodsService;
import com.example.demo.service.goods.dto.GoodsInfoDto;
import com.example.demo.service.goods.vo.GoodsInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
// todo v2的意思是版本号的意思,通常来自项目重构时和v1版本同时运行,防止项目不稳定
@RequestMapping("/api/v2/goods")
@CrossOrigin(origins = "*")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping
    public ResponseEntity<GoodsInfo> addGoods (@RequestBody @Valid GoodsInfoDto goodsInfoDto){
    System.out.println(goodsInfoDto);
    return ResponseEntity.ok(goodsService.addGoods(goodsInfoDto));

    }

    @Operation(summary = "查询单个商品接口", description = "根据商品id查询商品")
    @GetMapping("/{id:\\d+}")
    public ResponseEntity<GoodsInfoVo> getGoodsById(@PathVariable  Long id) {
        if (Objects.isNull(id) || id <= 0) {
            return null ;
        }

        return ResponseEntity.ok(goodsService.getGoodsById(id)) ;
    }
    @DeleteMapping("/{id:\\d+}")
    public ResponseEntity<Boolean>  deleteGoodsById(@PathVariable  Long id) {
        if (Objects.isNull(id) || id <= 0) {
            return null ;
        }
        return ResponseEntity.ok(goodsService.deleteGoods(id));

    }
    @GetMapping("/page")
    public ResponseEntity<Page<GoodsInfo>> queryGoodsList(@RequestParam Long pageCurrent, @RequestParam Long pageSize){
        if (Objects.isNull(pageCurrent) || pageCurrent < 0) {
            pageCurrent = 0l;
        }
        if (Objects.isNull(pageSize) || pageSize < 0) {
            pageSize = 10l;
        }
        return ResponseEntity.ok(goodsService.queryUserList(pageCurrent, pageSize));
    }


    @GetMapping("/random")
    public ResponseEntity<Page<GoodsInfo>> random(@RequestParam Long pageCurrent, @RequestParam Long pageSize){
        if (Objects.isNull(pageCurrent) || pageCurrent < 0) {
            pageCurrent = 0l;
        }
        if (Objects.isNull(pageSize) || pageSize < 0) {
            pageSize = 10l;
        }

        return ResponseEntity.ok(goodsService.random(pageCurrent, pageSize));
    }




}
